﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="AddEditMember.aspx.cs" 
    Inherits="TRL.TRLAdmin.Members.AddEditMember" Trace="false" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <nav>
        <div id="jCrumbs" class="breadCrumb module">
            <ul>
                <li>
                    <a href="#"><i class="icon-home"></i></a>
                </li>
                <li>
                    <a href="ViewMembers.aspx">Membership Management</a>
                </li>
                <li>
                <%
                    if (OperationMode == Mode.EditMember)
                    {
                        Response.Write("Edit Member: " + DisplayName);
                    }
                    else
                    {
                        Response.Write("Add Member");
                    }
                %>
                </li>
            </ul>
        </div>
    </nav>

    <div class="page-header">
        <%
            if (OperationMode == Mode.EditMember)
            {
                Response.Write("<h1>Edit Member <small>" + DisplayName + "</small></h1>");
            }
            else
            {
                Response.Write("<h1>Add Member</h1>");
            } 
        %>
    </div>
    
    <div class="row">
        <div class="span6">
            <asp:Label ID="message" runat="server" style="display: block" Visible="false" EnableViewState="false"></asp:Label>
        </div>
    </div>

    <div class="row-fluid">
        <div class="span12">
            <asp:FormView ID="FormView1" runat="server" DataKeyNames="membershipId" 
                DataSourceID="LinqMemberDataSource" CssClass="well"
                OnItemCreated="SetupDefaultsForMember"
                OnItemInserting="ValidateMember"
                OnItemUpdating="MemberUpdating"
                DefaultMode="Edit"
                OnDataBound="FormView1_DataBound"
                EnableViewState="false">
                <EditItemTemplate>
                    <fieldset class="form-horizontal">
                        <div class="control-group">
                            <label class="control-label">Type</label>
                            <div class="controls">
                                <asp:DropDownList ID="membershipTypeTextBox" runat="server"
                                DataValueField="membershipTypeId" DataSourceID="MembershipTypes" 
                                DataTextField="typeName" 
                                SelectedValue='<%# Bind("membershipType") %>'
                                />
                                <asp:SqlDataSource ID="MembershipTypes" runat="server" 
                                ConnectionString="<%$ ConnectionStrings:trladminConnectionString %>" 
                                SelectCommand="SELECT [membershipTypeId], [typeName] FROM [MembershipTypes]">
                                </asp:SqlDataSource>
                            </div>
                        </div>

                        <div class="control-group">
                            <label class="control-label">First Name</label>
                            <div class="controls">
                                <asp:TextBox ID="firstNameTextBox" runat="server" Text='<%# Bind("firstName") %>' class="required" />
                            </div>
                        </div>
                    <div class="control-group">
                        <label class="control-label">Last Name</label>
                        <div class="controls">
                            <asp:TextBox ID="lastNameTextBox" runat="server" 
                                Text='<%# Bind("lastName") %>' class="required"/>
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label">Gender</label>
                        <div class="controls">
                            <asp:DropDownList ID="gender" runat="server" SelectedValue='<%# Bind("gender") %>'>
                                <asp:ListItem Value="">Not Specified</asp:ListItem>
                                <asp:ListItem Value="M">Male</asp:ListItem>
                                <asp:ListItem Value="F">Female</asp:ListItem>
                            </asp:DropDownList>
                        </div>
                    </div>


                    <div class="control-group">
                        <label class="control-label">Shirt Size</label>
                        <div class="controls">
                            <asp:DropDownList ID="shirtSize" runat="server" SelectedValue='<%# Bind("shirtSize") %>'>
                                <asp:ListItem Value="">Not Specified</asp:ListItem>
                                <asp:ListItem Value="XS">Extra Small</asp:ListItem>
                                <asp:ListItem Value="S">Small</asp:ListItem>
                                <asp:ListItem Value="M">Medium</asp:ListItem>
                                <asp:ListItem Value="L">Large</asp:ListItem>
                                <asp:ListItem Value="XL">Extra Large</asp:ListItem>
                            </asp:DropDownList>
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label">Date of Birth</label>
                        <div class="controls">
                            <asp:TextBox ID="DOB" runat="server" 
                                Text='<%# Bind("dob", "{0:d}") %>' Width="70" MaxLength="14"/>
                            <Juice:Datepicker ID="Datepicker3" runat="server" TargetControlID="DOB"
                                ButtonImage="/img/calendar-icon.png" ButtonImageOnly="true" ShowOn="both" ButtonText="Show date picker" />
                            <asp:ControlParameter ControlID="DOB" DefaultValue="NULL" ConvertEmptyStringToNull="true" PropertyName="DOB" Name="DOB" Direction="Input" Type="DateTime" />
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label">Occupation</label>
                        <div class="controls"><asp:TextBox ID="TextBox1" runat="server" 
                            Text='<%# Bind("occupation") %>'/>
                        </div>
                    </div

                    <div class="control-group">
                        <label class="control-label">Second Person First Name</label>
                        <div class="controls"><asp:TextBox ID="secondFirstNameTextBox" runat="server" 
                            Text='<%# Bind("secondFirstName") %>'  class="required"/>
                            </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">Second Person Last Name</label>
                        <div class="controls"><asp:TextBox ID="secondLastNameTextBox" runat="server" 
                            Text='<%# Bind("secondLastName") %>'  class="required"/>
                         </div>
                    </div>
        
                    <div class="control-group">
                        <label class="control-label">Second Person Gender</label>
                        <div class="controls">
                            <asp:DropDownList ID="secondPersonGender" runat="server" SelectedValue='<%# Bind("secondGender") %>'>
                                <asp:ListItem Value="">Not Specified</asp:ListItem>
                                <asp:ListItem Value="M">Male</asp:ListItem>
                                <asp:ListItem Value="F">Female</asp:ListItem>
                            </asp:DropDownList>
                        </div>
                    </div>


                    <div class="control-group">
                        <label class="control-label">Second Person Shirt Size</label>
                        <div class="controls">
                              <asp:DropDownList ID="secondPersonShirtSize" runat="server" SelectedValue='<%# Bind("secondShirtSize") %>'>
                                <asp:ListItem Value="">Not Specified</asp:ListItem>
                                <asp:ListItem Value="XS">Extra Small</asp:ListItem>
                                <asp:ListItem Value="S">Small</asp:ListItem>
                                <asp:ListItem Value="M">Medium</asp:ListItem>
                                <asp:ListItem Value="L">Large</asp:ListItem>
                                <asp:ListItem Value="XL">Extra Large</asp:ListItem>
                            </asp:DropDownList>
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label">Second Person DOB</label>
                        <div class="controls">
                            <asp:TextBox ID="secondDob" runat="server" 
                                Text='<%# Bind("secondDob", "{0:d}") %>' Width="70" MaxLength="14" />
                            <Juice:Datepicker ID="Datepicker4" runat="server" TargetControlID="secondDob"
                                ButtonImage="/img/calendar-icon.png" ButtonImageOnly="true" ShowOn="both" ButtonText="Show date picker" />
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label">Second Person Occupation</label>
                        <div class="controls"><asp:TextBox ID="secondOccupation" runat="server" 
                            Text='<%# Bind("secondOccupation") %>'/>
                            </div>
                    </div

                    <div class="control-group">
                        <label class="control-label">Expiration</label>
                        <div class="controls">
                        <asp:TextBox ID="membershipExpirationTextBox" runat="server" 
                            Text='<%# Bind("membershipExpiration", "{0:d}") %>' Width="70" MaxLength="14" class="required" />
                        <Juice:Datepicker ID="Datepicker1" runat="server" TargetControlID="membershipExpirationTextBox"
                            ButtonImage="/img/calendar-icon.png" ButtonImageOnly="true" ShowOn="both" ButtonText="Show date picker" />
                        </div>
                    </div>
        
                    <div class="control-group"><label class="control-label">
                    Member Since</label><div class="controls">
                    <asp:TextBox ID="memberSinceTextBox" runat="server" 
                        Text='<%# Bind("memberSince", "{0:d}") %>' Width="70" MaxLength="14" class="required" />
                    <Juice:Datepicker ID="Datepicker2" runat="server" TargetControlID="memberSinceTextBox"
                        ButtonImage="/img/calendar-icon.png" ButtonImageOnly="true" ShowOn="both" ButtonText="Show date picker"  />
                    </div></div>

                    <div class="control-group"><label class="control-label">
                    Address</label><div class="controls">
                    <asp:TextBox ID="addressTextBox" runat="server" 
                        Text='<%# Bind("address") %>' class="required"/>
                    </div></div><div class="control-group"><label class="control-label">
                    City</label><div class="controls">
                    <asp:TextBox ID="cityTextBox" runat="server" 
                        Text='<%# Bind("city") %>' class="required" />
                    </div></div><div class="control-group"><label class="control-label">
                    State</label><div class="controls">
                    <asp:DropDownList ID="stateSelector" runat="server" SelectedValue='<%# Bind("state") %>'>
                        <asp:ListItem Value="AK">Alaska</asp:ListItem>
                        <asp:ListItem Value="AL">Alabama</asp:ListItem>
                        <asp:ListItem Value="AR">Arkansas</asp:ListItem>
                        <asp:ListItem Value="AZ">Arizona</asp:ListItem>
                        <asp:ListItem Value="CA">California</asp:ListItem>
                        <asp:ListItem Value="CO">Colorado</asp:ListItem>
                        <asp:ListItem Value="CT">Connecticut</asp:ListItem>
                        <asp:ListItem Value="DC">District of Columbia</asp:ListItem>
                        <asp:ListItem Value="DE">Delaware</asp:ListItem>
                        <asp:ListItem Value="FL">Florida</asp:ListItem>
                        <asp:ListItem Value="GA">Georgia</asp:ListItem>
                        <asp:ListItem Value="HI">Hawaii</asp:ListItem>
                        <asp:ListItem Value="IA">Iowa</asp:ListItem>
                        <asp:ListItem Value="ID">Idaho</asp:ListItem>
                        <asp:ListItem Value="IL">Illinois</asp:ListItem>
                        <asp:ListItem Value="IN">Indiana</asp:ListItem>
                        <asp:ListItem Value="KS">Kansas</asp:ListItem>
                        <asp:ListItem Value="KY">Kentucky</asp:ListItem>
                        <asp:ListItem Value="LA">Louisiana</asp:ListItem>
                        <asp:ListItem Value="MA">Massachusetts</asp:ListItem>
                        <asp:ListItem Value="MD">Maryland</asp:ListItem>
                        <asp:ListItem Value="ME">Maine</asp:ListItem>
                        <asp:ListItem Value="MI">Michigan</asp:ListItem>
                        <asp:ListItem Value="MN">Minnesota</asp:ListItem>
                        <asp:ListItem Value="MO">Missouri</asp:ListItem>
                        <asp:ListItem Value="MS">Mississippi</asp:ListItem>
                        <asp:ListItem Value="MT">Montana</asp:ListItem>
                        <asp:ListItem Value="NC">North Carolina</asp:ListItem>
                        <asp:ListItem Value="ND">North Dakota</asp:ListItem>
                        <asp:ListItem Value="NE">Nebraska</asp:ListItem>
                        <asp:ListItem Value="NV">Nevada</asp:ListItem>
                        <asp:ListItem Value="NH">New Hampshire</asp:ListItem>
                        <asp:ListItem Value="NJ">New Jersey</asp:ListItem>
                        <asp:ListItem Value="NM">New Mexico</asp:ListItem>
                        <asp:ListItem Value="NY">New York</asp:ListItem>
                        <asp:ListItem Value="OH">Ohio</asp:ListItem>
                        <asp:ListItem Value="OK">Oklahoma</asp:ListItem>
                        <asp:ListItem Value="OR" Selected=True>Oregon</asp:ListItem>
                        <asp:ListItem Value="PA">Pennsylvania</asp:ListItem>
                        <asp:ListItem Value="RI">Rhode Island</asp:ListItem>
                        <asp:ListItem Value="SC">South Carolina</asp:ListItem>
                        <asp:ListItem Value="SD">South Dakota</asp:ListItem>
                        <asp:ListItem Value="TN">Tennessee</asp:ListItem>
                        <asp:ListItem Value="TX">Texas</asp:ListItem>
                        <asp:ListItem Value="UT">Utah</asp:ListItem>
                        <asp:ListItem Value="VA">Virginia</asp:ListItem>
                        <asp:ListItem Value="VT">Vermont</asp:ListItem>
                        <asp:ListItem Value="WA">Washington</asp:ListItem>
                        <asp:ListItem Value="WI">Wisconsin</asp:ListItem>
                        <asp:ListItem Value="WV">West Virginia</asp:ListItem>
                        <asp:ListItem Value="WY">Wyoming</asp:ListItem>        </asp:DropDownList>        
                    </div></div><div class="control-group"><label class="control-label">
                    Zip</label><div class="controls">    
                    <asp:TextBox ID="postalCodeTextBox" runat="server" 
                        Text='<%# Bind("postalCode") %>' Width="40" MaxLength="5" class="required" />
                    </div></div><div class="control-group"><label class="control-label">
                    Phone</label><div class="controls">
                    <asp:TextBox ID="phoneNumberTextBox" runat="server" 
                        Text='<%# Bind("phoneNumber") %>' Width="80" MaxLength="10" class="phoneUS" />
                    </div></div>
                    <div class="control-group"><label class="control-label">Email</label><div class="controls">
                    <asp:TextBox ID="emailTextBox" runat="server" 
                        Text='<%# Bind("email") %>' class="required email" />
                    </div></div>
                    <div class="control-group"><label class="control-label">Second Person Email</label><div class="controls">
                    <asp:TextBox ID="secondEmailTextBox" runat="server" 
                        Text='<%# Bind("secondEmail") %>' class="email" />
                    </div></div>
            
                    <div class="control-group"><label class="control-label">
                    Forum Username</label><div class="controls">
                    <asp:TextBox ID="forumUsernameTextBox" runat="server" 
                        Text='<%# Bind("forumUsername") %>' />
                    </div></div>
                    <div class="control-group"><label class="control-label">Second Forum Username</label><div class="controls">
                    <asp:TextBox ID="secondForumUsernameTextBox" runat="server" 
                        Text='<%# Bind("secondForumUsername") %>' />
                    </div></div>

                    <div class="control-group"><label class="control-label">
                    Notes</label><div class="controls">
                    <asp:TextBox ID="notesTextBox" runat="server" Width="300" Height="200" TextMode="MultiLine"
                        Text='<%# Bind("notes") %>' />
                    </div></div>
                        <div class="control-group"><label class="control-label">
                    Send E-Newsletter</label><div class="controls">
                    <asp:CheckBox ID="sendENewsletterCheckBox" runat="server" 
                        Checked='<%# Bind("sendENewsletter") %>' />
                    </div></div>
                    <div class="control-group"><label class="control-label">
                    Okay to contact</label><div class="controls">
                    <asp:CheckBox ID="okayToContactCheckBox" runat="server" 
                        Checked='<%# Bind("okayToContact") %>' />
                    </div></div>
                            
                    
                    </fieldset>

                    <asp:Button Visible="true" ID="UpdateButton" runat="server" Text="Update" 
                        CssClass="btn btn-primary" CausesValidation="True" CommandName="Update" />

                    <asp:Button Visible="true" ID="InsertButton" runat="server" Text="Add and Mark as Needing Member Kit" 
                        CssClass="btn btn-primary" CausesValidation="True" CommandName="Insert" 
                        />
                        
                    <asp:Button ID="CancelButton" runat="server" Text="Cancel" 
                        CssClass="btn btn-danger cancel" CausesValidation="False" CommandName="Cancel" 
                        onclick="CancelButton_Click"/>

                    <div style="margin-top:10px">
                        <asp:Button Visible="true" ID="UpdateAndSendMemKitButton" runat="server" Text="Update and Mark as Needing Member Kit" 
                            CssClass="btn btn-primary" CausesValidation="True" CommandName="Update" CommandArgument="UpdateAndMarkNeedWelcomeKit" />

                        <asp:Button Visible="true" ID="UpdateAndMarkMemKitSent" runat="server" Text="Update and Mark as Member Kit Sent" 
                            CssClass="btn btn-primary" CausesValidation="True" CommandName="Update" CommandArgument="MarkWelcomeKitSent" />
                    </div>

                    <asp:Panel runat="server" ID="MembershipKitHistoryFields" Visible="false" style="margin-top: 15px">
                        <div class="control-group">
                            <b>Member Kit Last Sent On: <%# Eval("welcomeKitLastSentOn", "{0:d}")%></b>
                        </div>
                        <div class="control-group">
                            <b>Member Kit Last Sent By: <%# Eval("welcomeKitLastSentByFullName", "{0:d}")%></b>
                        </div>
                    </asp:Panel>
                </EditItemTemplate>
            </asp:FormView>
            <asp:LinqDataSource ID="LinqMemberDataSource" runat="server" 
                ContextTypeName="TRL.DataAccessLayer.DataClasses1DataContext"
                EnableInsert="True" 
                EnableUpdate="True" 
                EnableDelete="True" 
                EntityTypeName="" 
                TableName="Members"
                OnInserted="MemberInsertedOrUpdated"
                OnUpdated="MemberInsertedOrUpdated"
                Where="membershipId = @membershipId"
                OnSelecting="MemberSelecting"
                OnSelected="MemberSeleced"
                EnableViewState="false">
            </asp:LinqDataSource>
        </div>
    </div>

</asp:Content>

    
<asp:Content ID="Content3" ContentPlaceHolderID="scripts" runat="server">
    <script type="text/javascript">
        // Add a JQuery Validator for phone number
        jQuery.validator.addMethod("phoneUS", function (phone_number, element) {
            phone_number = phone_number.replace(/\s+/g, "");
            return this.optional(element) || phone_number.length > 9 && phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
        }, "Please specify a valid phone number");

        $(document).ready(function () {
            // Set the sidebar orientation to the section and page 
            // of this document
            $('#collapseOne').addClass("in");

            <%
                // Highlight in the sidebar the approriate page based on the mode we are in
                if (OperationMode == Mode.AddMember)
                    Response.Write("$('#addMember').addClass(\"active\");");
                else
                    Response.Write("$('#viewMembers').addClass(\"active\");");                    
            %>

            // Enable\disable the fields associated with individual and family memberships
            // based on the Type selection
            enableDisableSecondPersonFieldsByMember();

            // Setup the validator of the form
            $('#form1').validate({
                highlight: function (element) {
                    $(element).closest('div').addClass("f_error");
                    $(element).closest('div').parent('div').addClass("error");
                },
                unhighlight: function (element) {
                    $(element).closest('div').removeClass("f_error");
                    $(element).closest('div').parent('div').removeClass("error");
                },
                errorPlacement: function (error, element) {
                    // Not sure why but jquery validator is adding a "display: block" style to the label it inserst.
                    // Below line ensures it's inline. Note that I have to do this here as the class already specifically states
                    // it's inline.
                    error.css({ 'display': 'inline' });
                    $(element).closest('div').append(error);
                }
            });
        });


        function enableDisableSecondPersonFieldsByMember() {
            // If an individual membership was selected disable the Second Person fields
            if ($('#ContentPlaceHolder1_FormView1_membershipTypeTextBox').val() == 1) {

                $('#ContentPlaceHolder1_FormView1_secondFirstNameTextBox').attr('disabled', 'disabled');
                $('#ContentPlaceHolder1_FormView1_secondLastNameTextBox').attr('disabled', 'disabled');
                $('#ContentPlaceHolder1_FormView1_secondEmailTextBox').attr('disabled', 'disabled');
                $('#ContentPlaceHolder1_FormView1_secondForumUsernameTextBox').attr('disabled', 'disabled');

                $('#ContentPlaceHolder1_FormView1_secondPersonGender').attr('disabled', 'disabled');
                $('#ContentPlaceHolder1_FormView1_secondPersonShirtSize').attr('disabled', 'disabled');
                $('#ContentPlaceHolder1_FormView1_secondDob').attr('disabled', 'disabled');
                $('#ContentPlaceHolder1_FormView1_secondOccupation').attr('disabled', 'disabled');

            } else {

                $('#ContentPlaceHolder1_FormView1_secondFirstNameTextBox').removeAttr('disabled');
                $('#ContentPlaceHolder1_FormView1_secondLastNameTextBox').removeAttr('disabled');
                $('#ContentPlaceHolder1_FormView1_secondEmailTextBox').removeAttr('disabled');
                $('#ContentPlaceHolder1_FormView1_secondForumUsernameTextBox').removeAttr('disabled');

                $('#ContentPlaceHolder1_FormView1_secondPersonGender').removeAttr('disabled');
                $('#ContentPlaceHolder1_FormView1_secondPersonShirtSize').removeAttr('disabled');
                $('#ContentPlaceHolder1_FormView1_secondDob').removeAttr('disabled');
                $('#ContentPlaceHolder1_FormView1_secondOccupation').removeAttr('disabled');

            }
        }

        $('#ContentPlaceHolder1_FormView1_membershipTypeTextBox').click(function () {
            enableDisableSecondPersonFieldsByMember();
        });


    </script>
</asp:Content>

